package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.entity.TreeResult;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<TreeResult> roleList = roleService.findAll();
            return new Result(true,MessageConstant.GET_ROLE_SUCCESS,roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLE_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.findPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    @RequestMapping("/findByRoleId")
    public Result findByRoleId(Integer id) {
        try {
            Map map = roleService.findByRoleId(id);
            return new Result(true,MessageConstant.GET_ROLE_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ROLE_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true,MessageConstant.GET_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ROLE_FAIL);
        }
    }

    @RequestMapping("/findAllByRoleId")
    public Result findAllByRoleId() {
        try {
            Map map = roleService.findAllByRoleId();
            return new Result(true,MessageConstant.GET_ROLE_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ROLE_FAIL);
        }
    }

    @RequestMapping("/findIdByRoleId")
    public Result findIdByRoleId(Integer id){
        try {
            Map map = roleService.findIdByRoleId(id);
            return new Result(true,MessageConstant.GET_ROLE_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ROLE_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(Integer[] permissionIds,Integer[] menus,@RequestBody Role role) {
        try {
            roleService.edit(permissionIds,menus,role);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    @RequestMapping("/getRoleList")
    public Result getRoleList(String name) {
        try {
            List<Role> list = roleService.getRoleList(name);
            return new Result(true,MessageConstant.GET_ROLE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.GET_ROLE_FAIL);
        }
    }
}
